import axios from '../../../controllers/request'
/* 工作流 */

/**
 * 列表
 */
export async function workflowListApi(page = 1, pageSize = 20, name = '', category = '') {
  try {
    const response: { data: any[]; total: number } = await axios.get(
      `/api/v1/workflow/list?page=${page}&limit=${pageSize}&name=${name}&category_code=${category}`
    )
    return response
  } catch (error) {
    console.error(error)
    throw error
  }
}

export async function workflowPublishListApi(page = 1, pageSize = 20, name = '', category = '') {
  try {
    const response: { data: any[]; total: number } = await axios.get(
      `/api/v1/workflow/list?status=${1}&page=${page}&limit=${pageSize}&name=${name}&category_code=${category}`
    )
    return response
  } catch (error) {
    console.error(error)
    throw error
  }
}
/**
 * 新增
 */
export const workflowCreateApi = async data => {
  return await axios.post(`/api/v1/workflow/create`, data)
}

/**
 * 编辑
 */
export const workflowEditApi = async (flow_id, data) => {
  return await axios.post(`/api/v1/workflow/update/${flow_id}`, data)
}

/* 
获取工作流在智能体中被引用数
*/
export async function flowUsedCountByAstApi(id) {
  return await axios.get(`/api/v1/assistant/used_flow_count?id=${id}`)
}

/* 
获取知识库在工作流中被引用数
*/
export async function knowledgeUsedCountByFlowApi(id) {
  return await axios.get(`/api/v1/workflow/used_knowledge_count?id=${id}`)
}

/* 
获取插件在工作流中被引用数
*/
export async function toolUsedCountByFlowApi(id) {
  return await axios.get(`/api/v1/workflow/used_tool_count?id=${id}`)
}

/* 删除 */
export const workflowDelApi = async flow_id => {
  return await axios.post(`/api/v1/workflow/delete/${flow_id}`, {})
}

/* 详情 */
export async function getWorkFlowDetailApi(flowId: string): Promise<any> {
  return axios.get(`/api/v1/workflow/detail/${flowId}`)
}

/* 发布 */
export const workflowPublishApi = async flow_id => {
  return await axios.post(`/api/v1/workflow/publish/${flow_id}`, {})
}

/* 取消发布 */
export const workflowUnpublishApi = async flow_id => {
  return await axios.post(`/api/v1/workflow/unpublish/${flow_id}`, {})
}

/* 单节点运行 */
export const singleNodeRun = async data => {
  return await axios.post(`/api/v1/workflow/node_run`, data)
}
